import axios from 'axios'
import router from '../router/Router'


function install(_vue) {
  axios.$vue = _vue;
  // axios 配置
  axios.defaults.timeout = 5000;

// http request 拦截器
  axios.interceptors.request.use(
    config => {
      config.headers.system = 'test1';
      if (axios.$vue.$store.state.user.token) {
        config.headers.token = axios.$vue.$store.state.user.token;
      }
      return config;
    },
    err => {
      return Promise.reject(err);
    });

// http response 拦截器
  axios.interceptors.response.use(
    response => {
      return response;
    },
    error => {
      if (error.response) {
        switch (error.response.status) {
          case 401:
            router.replace({
              path: 'login'
            })
        }
      }
      return Promise.reject(error.response.data)
    });
  return axios;
}


export default {
  install:install
}
